﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using IceTea.Resources;
using System.Threading;
using IceTea.Domain.Models;
using IceTea.Domain;

namespace IceTea.Controllers
{
    /// <summary>
    /// This is the base class for all the Controllers in this web app
    /// </summary>
    public class BaseController : Controller
    {
        protected IceTeaPrincipal IceTeaUser
        {
            get
            {
                return (IceTeaPrincipal)User;
            }
        }
        
        protected void SetCurrentCulture(HttpContextBase httpContext)
        {
            CultureInfo culture = null;

            //retrieve the culture info from cookie if it was set
            HttpCookie cookie = httpContext.Request.Cookies["_culture"];
            if (cookie != null)
            {
                culture = CultureHelper.GetValidCulture(cookie.Value);
            }
            else
            {
                //read the prefered language info from the request header (set in browser) if there is no cookie
                culture = CultureHelper.GetValidCulture(httpContext.Request.UserLanguages[0]);
            }

            //Set the appropriate culture to the current thread
            Thread.CurrentThread.CurrentUICulture = culture;
            Thread.CurrentThread.CurrentCulture = culture;
        }

        protected void SetRoleProfile(HttpContextBase httpContext)
        {
            //Retrieve current user role profile
            if (httpContext.User.Identity.IsAuthenticated)
            {   
                //if there is no RoleProfile in session then create it
                if (httpContext.Session["UserRoleProfile"] == null)
                {
                    try
                    {
                        //Get the RoleProfile and save in session
                        httpContext.Session["UserRoleProfile"] = RoleProfile.ReadRoleProfile(httpContext.User.Identity.Name);
                        
                    }
                    catch (ArgumentException)
                    {
                        //user name is invalid, need log error here
                    }                    
                }
                IceTeaPrincipal principal = new IceTeaPrincipal(httpContext.User);
                principal.RoleProfile = httpContext.Session["UserRoleProfile"] as RoleProfile;
                httpContext.User = principal;

            }
        }

        protected void ClearRoleProfile(HttpContextBase httpContext)
        {
            if (httpContext.Session["UserRoleProfile"] != null)
                httpContext.Session["UserRoleProfile"] = null;
        }

        protected void SwitchProject(int projectId)
        {
            
        }
        
        protected override IAsyncResult BeginExecute(System.Web.Routing.RequestContext requestContext, AsyncCallback callback, object state)
        {
            SetCurrentCulture(requestContext.HttpContext);
            SetRoleProfile(requestContext.HttpContext);
           
            return base.BeginExecute(requestContext, callback, state);
        }
    }
}
